//
// Created by admin on 2021/2/15.
//

#ifndef ANDROIDGRAPHICSDEMOS_RENDERITEMINSTANCED_H
#define ANDROIDGRAPHICSDEMOS_RENDERITEMINSTANCED_H
#include "RenderItem.h"
#include <memory>
#include <vector>
#include <gles3/gl32.h>
#include <glm.hpp>
#include "VertexAttrib.h"
#include "GLESUtils.h"
#include "ComputeParticles.h"


class Buffer;
class Texture;
class Model;

namespace Phy
{
    class AABB;
}

template<GraphicsAPI api> class RenderItemInstanced;


template<>
class RenderItemInstanced<GraphicsAPI::GLESv3> : public IRenderItem, public IGLESRenderItem<RenderItemInstanced<GraphicsAPI::GLESv3>>{

public:

    RenderItemInstanced(int number, const Phy::AABB & bounding_box, DummyItemType dummy_type);
    RenderItemInstanced(int number, const glm::mat4 * matrix_vec,  DummyItemType dummy_type);
    RenderItemInstanced(int x_num, int y_num, int z_num, float leap, glm::vec3 start_point,  DummyItemType dummy_type);


    GLuint GetVAO() const {return vertexArrayObject;}

    int GetInstancedNumber () const {return instancedNumber;}

    std::vector<ParticleStructure> particleList;

protected:

    std::vector<std::pair<VertexAttribType, std::shared_ptr<Buffer>>> vertexAttribBufferList; // 顶点属性 buffer 列表
    std::shared_ptr<Buffer> vertexIndexBuffer;    // 顶点index buffer

    std::shared_ptr<Buffer> instanceMatrixBuffer;
    std::vector<glm::mat4> instanceMatrixVector;

    DummyItemType dummy_type;

    int vertexNumber{};
    int faceNumber{};
    int instancedNumber{};

    void InitStructures();
    void AppendVertexAttribData(VertexAttribType attrib_type, const void * data, uint size, uint strip, uint offset);
    void SetElementData(const void * element_data, size_t element_data_length);
    GLuint vertexArrayObject{};
};


#endif //ANDROIDGRAPHICSDEMOS_RENDERITEMINSTANCED_H
